(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:authorizationCtrl
     * @description
     * # authorizationCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('AuthorizationCtrl', AuthorizationCtrl);

    AuthorizationCtrl.$inject = ['$scope', 'dataService', '$location', '$state', 'helper', 'sessionService', 'moment'];

    function AuthorizationCtrl($scope, dataService, $location, $state, helper, sessionService, moment) {

        /**
         * listAction   显示
         */
        //显示
        $scope.listAction = function() {
            $scope.getAuthorizationList(1);
            // $scope.id = "1";
            $scope.pageSize = "10";
        }

        //获取列表
        $scope.getAuthorizationList = function(pageNo) {
                var query = { page_no: pageNo ? pageNo : "1", page_size: $scope.pageSize };
                query.query = { distributor_id: dataService.currentLoginedUser().partner_id, distributor_nick: sessionService.get("currentUserInfo").user_name };
                // if ($scope.keyword) {
                //     query.query = { title: $scope.keyword };
                // } supplier_agree: 1, seller_agree: 1
                dataService.sellercooperationGet(query).then(function(list) {
                    if (list) {
                        $scope.authorizationlist = list.shop;
                        $scope.authorizationTotal = list.total_count;
                    }
                });
            }
            //选中供应商
        $scope.addfenxiao = function(supplier) {
                if (supplier) {
                    $scope.authorization.supplier_id = supplier.company_id;
                    $scope.authorization.supplier_nick = supplier.default_admin;
                }
            }
            //新增授权关系
        $scope.addAction = function() {
            var id = $location.search().id;
            $scope.authorization = {};
            if (id) {
                dataService.sellercooperationOne({ cooperate_id: id }).then(function(list) {
                    if (list) {
                        $scope.authorization = list;
                        // $scope.authorizationTotal = list.total_count;
                    }
                });
            }
        }

        $scope.addAuthorization = function() {
            if ($scope.authorization) {
                if ($scope.authorization.cooperate_id) {
                    dataService.sellercooperationUpdate($scope.authorization).then(function(r) {
                        if (r.Msg) {
                            helper.myAlert("error:" + r.Msg);
                        } else {
                            helper.myAlert("success", null, null, function() {
                                $state.go("authorization.list");
                            });
                        }
                    });
                } else {
                    var cuser = dataService.currentLoginedUser();
                    $scope.authorization.distributor_id = cuser.partner_id;
                    $scope.authorization.distributor_nick = cuser.user_name;
                    $scope.authorization.seller_agree = true;
                    dataService.sellercooperationAdd($scope.authorization).then(function(r) {
                        if (r.Msg) {
                            helper.myAlert("error:" + r.Msg);
                        } else {
                            helper.myAlert("success", null, null, function() {
                                $state.go("authorization.list");
                            });
                        }
                    });
                }
            } else {
                helper.myAlert("error: No data found");
            }
        }

        $scope.applylistAction = function() {

            }
            //同意 供应商的申请
        $scope.Consentauthorization = function(c_id) {
                if (c_id) {
                    helper.myConfirm('Agree with the supplier Application', function() {
                        var updata = { cooperate_id: c_id, seller_agree: true, seller_agree_time: moment().format('YYYY-MM-DD HH:mm:ss') };
                        dataService.sellercooperationUpdate(updata).then(function(r) {
                            if (r.Msg) {
                                helper.myAlert("error:" + r.Msg);
                            } else {
                                helper.myAlert("success", null, null, function() {
                                    $scope.getAuthorizationList();
                                });
                            }
                        });
                    });
                }
            }
            //删除分销关系
        $scope.delauthorization = function(c_id) {
            if (c_id) {
                var cooperation = { "cooperate_id": c_id };
                helper.myConfirm('confirm operation', function() {
                    dataService.sellercooperationDel(cooperation).then(
                        function(re) {
                            if (re.Msg) {
                                helper.myAlert("error:" + r.Msg);
                            } else {
                                helper.myAlert("success", null, null, function() {
                                    $scope.listAction();
                                });
                            }
                        }
                    );
                });
            }
        }

        //授权关系商品 列表
        $scope.itemAction = function(isid) {
             $scope.id = $location.search().id ? $location.search().id : isid;
            $scope.authorization = {};
            if ( $scope.id) {
                dataService.sellercooperationOne({ cooperate_id:  $scope.id }).then(function(list) {
                    if (list) {
                        $scope.authorization = list;
                        if (list.product_line) {
                            var product_idAry = list.product_line.split(",");
                            var product_nameAry = list.product_line_name.split(",");
                        }
                        $scope.productAry = [];
                        _.map(product_idAry, function(da, ke) {
                            $scope.productAry.push({ 'id': da, 'name': product_nameAry[ke] });
                        });
                    }
                });
            }
        }

        //下载商品
        $scope.downloadItme = function(id, name) {
            if (id) {
                $scope.itemtype = id;
                $scope.itemName = name;
                var query = { "productcat_id": id }
                dataService.productGet({ query: query }).then(function(the) {
                    if (!the.Msg) {
                        $scope.itmeAry = the.products;
                    }
                })
            }
        }

        //使用商品
        $scope.useItem = function(item) {
            if (item.pid) {
                dataService.productUpdate({ "pid": item.pid }).then(function(the) {
                    if (!the.Msg) {
                        item.istype = the.item_id
                        helper.myAlert("success");
                    }
                })
            }
        }
    }
})();
